#include <gtest/gtest.h>
#include <string>
#include <chrono>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <regex>

#include "loger.hpp"

class LoggerInfoTest : public ::testing::Test {
  protected:
    void SetUp() override
    {
        sl::loger::__detail::init();
    }

    void TearDown() override { }
};

// 测试用例1: 当日志级别高于INFO时，info函数应该不执行任何操作
TEST_F(LoggerInfoTest, ShouldNotLogWhenLevelIsHigherThanInfo)
{
    sl::loger::setLevel(sl::loger::Level::LOG_WARN);

    testing::internal::CaptureStdout();
    sl::loger::info("This is a test message.");
    std::string output = testing::internal::GetCapturedStdout();

    // 验证没有输出
    EXPECT_EQ(output, "");
}

// 测试用例2: 当日志级别等于INFO时，info函数应该执行日志记录
TEST_F(LoggerInfoTest, ShouldLogWhenLevelEqualsInfo)
{
    sl::loger::setLevel(sl::loger::Level::LOG_INFO);

    testing::internal::CaptureStdout();
    sl::loger::info("Test message");
    std::string output = testing::internal::GetCapturedStdout();

    // Assert
    EXPECT_NE(output.find("[info] Test message"), std::string::npos);
}

// 测试用例3: 当日志级别低于INFO时，info函数应该执行日志记录
TEST_F(LoggerInfoTest, ShouldLogWhenLevelIsLowerThanInfo)
{
    sl::loger::setLevel(sl::loger::Level::LOG_DEBUG);

    testing::internal::CaptureStdout();
    sl::loger::info("Debug message should be logged as info too");
    std::string output = testing::internal::GetCapturedStdout();

    EXPECT_NE(output.find("[info] Debug message should be logged as info too"),
        std::string::npos);
}

// 测试用例4: 带参数的格式化消息测试
TEST_F(LoggerInfoTest, ShouldHandleFormattedMessageWithArgs)
{
    sl::loger::setLevel(sl::loger::Level::LOG_INFO);

    testing::internal::CaptureStdout();
    sl::loger::info("User {} logged in with ID {}", "John", 12345);
    std::string output = testing::internal::GetCapturedStdout();

    // Assert
    EXPECT_NE(output.find("[info] User John logged in with ID 12345"),
        std::string::npos);
}

// 测试用例5: 空消息测试
TEST_F(LoggerInfoTest, ShouldHandleEmptyMessage)
{
    sl::loger::setLevel(sl::loger::Level::LOG_INFO);

    testing::internal::CaptureStdout();
    sl::loger::info("");
    std::string output = testing::internal::GetCapturedStdout();

    // Assert
    EXPECT_NE(output.find("[info] "), std::string::npos);
}

// 测试用例6: 特殊字符消息测试
TEST_F(LoggerInfoTest, ShouldHandleSpecialCharactersInMessage)
{
    sl::loger::setLevel(sl::loger::Level::LOG_INFO);

    testing::internal::CaptureStdout();
    sl::loger::info("Special chars: \n\t\r\\\"'");
    std::string output = testing::internal::GetCapturedStdout();

    // Assert
    EXPECT_NE(
        output.find("[info] Special chars: \n\t\r\\\"'"), std::string::npos);
}

// 性能测试用例
TEST_F(LoggerInfoTest, PerformanceTestMultipleCalls)
{
    sl::loger::setLevel(sl::loger::Level::LOG_INFO);
    const int iterations = 1000;

    testing::internal::CaptureStdout();
    for(int i = 0; i < iterations; ++i) {
        sl::loger::info("Performance test message");
    }
    std::string output = testing::internal::GetCapturedStdout();

    // Assert - 验证输出包含预期的消息数量
    int    count = 0;
    size_t pos   = 0;
    while((pos = output.find("[info] Performance test message", pos)) !=
          std::string::npos) {
        ++count;
        pos += 10; // 移动到下一个位置
    }
    EXPECT_EQ(count, iterations);
}